package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.ClassDetailsData;
import com.eduhzy.ttw.commonsdk.entity.ClassTeacherData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerClassTransferComponent;
import com.eduhzy.ttw.teacher.mvp.contract.ClassTransferContract;
import com.eduhzy.ttw.teacher.mvp.presenter.ClassTransferPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-7-17 10:51:03
 */
@Route(path = RouterHub.APP_CLASSTRANSFERACTIVITY)
public class ClassTransferActivity extends BaseActivity<ClassTransferPresenter> implements ClassTransferContract.View {
    @BindView(R.id.public_toolbar_iv_right)
    ImageView ivRight;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @BindView(R.id.public_toolbar_right_iv_layout)
    AutoRelativeLayout right;
    @Autowired(name = Constants.ROUTER_DATA)
    ClassDetailsData mineClassData;
    @Inject
    BaseQuickAdapter<ClassTeacherData, AutoBaseViewHolder> mAdapter;
    @Inject
    List<ClassTeacherData> lists;
    @Inject
    RecyclerView.LayoutManager layoutManager;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerClassTransferComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_class_transfer;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        mSwipeLayout.setOnRefreshListener(() -> {
                    mAdapter.removeAllFooterView();
                    mPresenter.getTeacherByClass(mineClassData.getId());
                }
        );

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            ClassTeacherData data = mAdapter.getData().get(position);
            if (data.isBzr()) {
                showMessage("不能转让给班主任");
            } else {
                RxUtil.showConfirmDialog(getActivity(),
                        "确定将班主任转让给" + data.getRealName() + "吗",
                        (dialog, index) -> {
                            dialog.dismiss();
                            mPresenter.switchClassManager(mineClassData.getId(), SPUtils.getInstance().getString(Constants.USERID), data.getUserId());
                        });
            }
        });
        mPresenter.getTeacherByClass(mineClassData.getId());
    }

    @Override
    public void showLoading() {
        if (mSwipeLayout != null) {
            mSwipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (mSwipeLayout != null) {
            mSwipeLayout.setRefreshing(false);
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }
}
